package com.yanqu.road.server.cmd.estatewar.activity.cross;

import com.yanqu.road.entity.estatewar.palace.PalaceFloorConfig;
import com.yanqu.road.entity.estatewar.palace.UserPalaceData;
import com.yanqu.road.entity.log.LogPalacePos;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.palace.PalaceModule;
import com.yanqu.road.server.gameplayer.module.player.EstateWarModule;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.palace.PalaceActivity;
import com.yanqu.road.server.manger.palace.PalaceActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_PALACE_CROSS_BATTLE_RESP, desc = "跨服战斗返回")
public class CrossPalaceBattleResponseCmd extends AbstractCommand {
    private EstateWarProto.PalaceBattleRespMsg reqMsg;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = EstateWarProto.PalaceBattleRespMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(reqMsg.getRet() == 0) {
            PalaceActivity palaceActivity = PalaceActivityMgr.getInTimePalaceActivity();
            if(null != palaceActivity) {
                PalaceModule palaceModule = player.getModule(PalaceModule.class);
                UserPalaceData userPalaceData = palaceModule.getUserPalaceData();
                if (null != userPalaceData) {
                    if(reqMsg.getIsWin()) {
                        userPalaceData.setPos(reqMsg.getNowPos().getPos());
                    }
                    if (userPalaceData.getPos() > 0) {
                        PalaceFloorConfig palaceFloorConfig = palaceActivity.getPalaceFloorConfig(userPalaceData.getPos());
                        if(null != palaceFloorConfig) {
                            int estateSkinId = palaceFloorConfig.getSkinsReward();
                            if(estateSkinId > 0) {
                                EstateWarModule estateWarModule = player.getModule(EstateWarModule.class);
                                estateWarModule.unlockEstateSkin(estateSkinId);
                            }
                        }
                    }
                    palaceModule.syncUserData();
                    AutoLogMgr.add(new LogPalacePos(palaceActivity.getPalaceActivityId(), player.getUserId(), userPalaceData.getPos()));
                }
            }
        }

        player.sendPacket(Protocol.U_PALACE_BATTLE, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
